/*
Simple Forum Auto Poster.
Copyright (C) 2011 Gianni Van Hoecke.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package gui.history;

import domain.forumdata.Post;
import domain.net.BBCodeParser;
import java.awt.BorderLayout;
import javax.swing.DefaultListModel;
import persistence.OpenBrowser;

/**
 *
 * @author Gianni Van Hoecke <gianni.vh@gmail.com>
 */
public class HistoryItemDetails extends javax.swing.JPanel
{

    /** Creates new form HistoryItemDetails */
    public HistoryItemDetails()
    {
        initComponents();
        this.lstURLs.setModel(new DefaultListModel());
        BorderLayout layout = (BorderLayout) getLayout();
        layout.setVgap(3);
        layout.setHgap(3);
        this.jSplitPane1.setDividerLocation(350);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblInfo = new javax.swing.JLabel();
        jSplitPane1 = new javax.swing.JSplitPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtPost = new javax.swing.JTextPane();
        jScrollPane3 = new javax.swing.JScrollPane();
        lstURLs = new javax.swing.JList();

        setLayout(new java.awt.BorderLayout());

        lblInfo.setText("<html>Details:</html>");
        add(lblInfo, java.awt.BorderLayout.PAGE_START);

        jSplitPane1.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        jSplitPane1.setDividerLocation(114);
        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

        txtPost.setEditable(false);
        jScrollPane1.setViewportView(txtPost);

        jSplitPane1.setLeftComponent(jScrollPane1);

        lstURLs.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Links here..." };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        lstURLs.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lstURLsMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(lstURLs);

        jSplitPane1.setRightComponent(jScrollPane3);

        add(jSplitPane1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void lstURLsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lstURLsMouseClicked

        //Open browser on double click
        if(evt.getClickCount() == 2)
        {
            String selected = (String)this.lstURLs.getSelectedValue();
            if(selected != null)
                OpenBrowser.openURL(selected);
        }
        
    }//GEN-LAST:event_lstURLsMouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JLabel lblInfo;
    private javax.swing.JList lstURLs;
    private javax.swing.JTextPane txtPost;
    // End of variables declaration//GEN-END:variables

    public void loadItem(Post post)
    {
        //Post
        this.txtPost.setContentType("text/html");
        this.txtPost.setText(BBCodeParser.toHTML(post.getPost()));
        this.txtPost.setCaretPosition(0);

        //URLs
        DefaultListModel model = new DefaultListModel();
        for(String url : post.getTopicURLResults())
            model.addElement(url);
        this.lstURLs.setModel(model);
    }

    public void loadHistory()
    {
        this.txtPost.setText("");
        this.lstURLs.setModel(new DefaultListModel());
    }
}